package com.coolwen.spring.base;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @author CoolWen
 * @version 2020-03-05 9:55
 */
public class Base {
    public Base() {
        try {
            Properties pro = new Properties();
            pro.load(Base.class.getClassLoader().getResourceAsStream("beans.properties"));
            Method[] ms = this.getClass().getMethods();
            for (Method m : ms) {
                if (m.isAnnotationPresent(Inject.class)) {
                    String mname = m.getName();//setWeapon
                    mname = mname.substring(3);//Weapon
                    mname = mname.substring(0, 1).toLowerCase() + mname.substring(1);//weapon
                    System.out.println(mname);
                    String cname = (String) pro.get(mname);
                    Object o = Class.forName(cname).newInstance();
                    m.invoke(this, o);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
